<?php
/**
 * System_Model_MenuMapper
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: MenuMapper.php 906 2011-10-20 02:38:33Z mmxie $
 */
class System_Model_MenuMapper extends IG_ModelMapper
{
    /**
     * JSTREE 格式的数据
     * @param int $parentId
     * @return array
     */
    public function getByParentId($parentId)
    {
        return $this->getDbTable()->getByParentId($parentId);
    }

    public function toJstree($rows, $parentId)
    {
        $data = array($parentId => array('children' => array()));
        foreach ($rows as $row) {
            $data[$row['id']] = (isset($data[$row['id']]) ?  $data[$row['id']] : array())
                              + array(
                'data' => array(
                    'title' => $row['title'],
                    'attr' => array(
                        'href'  => (string) $row['url'],
                    ),
                ),
                'attr' => array(
                    'id'   => 'ig-jstree-' . $row['id'],
                    'rel'  => $row['type'],
                ),
            );

            $data[$row['parent_id']]['children'][] = & $data[$row['id']];
        }
        return $data[$parentId]['children'];
    }
}